package com.zbl.zblproject.core.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zbl.zblproject.core.entity.*;
import com.zbl.zblproject.core.mapper.*;
import com.zbl.zblproject.core.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2019/7/18
 */
@Api(tags = {"php接口"})
@RestController
@RequestMapping("/php")
public class PhpController {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private AdminUserMapper adminUserMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private CartItemMapper cartItemMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private RegionMapper regionMapper;

    @GetMapping("/purchase_list/{isPay}/{userId}")
    @ApiOperation("purchase_list")
    public Result purchaseLst(@PathVariable int isPay, @PathVariable int userId
            , @RequestParam("start") String start, @RequestParam("end") String end) {
        //isPay 0全部订单条数 1待付款订单 2未完成订单 3已完成订单 4取消订单
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        int count4 = 0;
        int count5 = 0;
        count1 = orderMapper.countOrders(userId, 0);
        count2 = orderMapper.countOrders(userId, 1);
        count3 = orderMapper.countOrders(userId, 2);
        count4 = orderMapper.countOrders(userId, 3);
        count5 = orderMapper.countOrders(userId, 4);
        List<Order> orders = orderMapper.getOrders(userId, isPay, start.trim(), end.trim());
        for(Order order:orders){
            QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("order_sn",order.getOrderSn());
            order.setOrderItems(orderItemMapper.selectList(queryWrapper));
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("count1", count1);
        jsonObject.put("count2", count2);
        jsonObject.put("count3", count3);
        jsonObject.put("count4", count4);
        jsonObject.put("count5", count5);
        jsonObject.put("orders", orders);
        return ResultUtil.success("成功", jsonObject);
    }

    @GetMapping("/order_attribute/{userId}")
    @ApiOperation("order_attribute")
    public Result orderAttribute(@PathVariable int userId) {
        List<UserAddress> addresses = userAddressMapper.selectAddressesByUserIds(userId);

        return ResultUtil.success("成功", addresses);
    }

    @GetMapping("/deleteAddress/{addressId}")
    @ApiOperation("deleteAddress")
    public Result deleteAddress(@PathVariable int addressId) {
        UserAddress userAddress = userAddressMapper.selectById(addressId);
        userAddress.setIsDel(1);
        int rs = userAddressMapper.updateById(userAddress);

        return ResultUtil.success(rs > 0 ? "成功" : "失败");
    }

    @PostMapping("/insertAddress")
    @ApiOperation("insertAddress")
    public Result insertAddress(@RequestParam("json") String json) {
        JSONArray jsonArray = JSONArray.parseArray(json);
        JSONObject jsonObject = jsonArray.getJSONObject(0);
        UserAddress userAddress = new UserAddress();
        userAddress.setAddress(jsonObject.getString("addrInfo"));
        userAddress.setMobile(jsonObject.getString("mobiles"));
        userAddress.setCity(jsonObject.getInteger("city"));
        userAddress.setDistrict(jsonObject.getInteger("districts"));
        userAddress.setProvince(jsonObject.getInteger("province"));
        userAddress.setZipcode(jsonObject.getString("zipcode"));
        userAddress.setConsignee(jsonObject.getString("consignees"));
        userAddress.setUserId(jsonObject.getInteger("userId"));
        int rs = userAddressMapper.insert(userAddress);

        return ResultUtil.success(rs > 0 ? "成功" : "失败");
    }

    @PostMapping("/countsCarts/{userId}")
    @ApiOperation("insertAddress")
    public Result countsCarts(@PathVariable int userId) {
        List<CartItem> cartItems = cartItemMapper.selectCartItemByUserId(userId);

        return ResultUtil.success("成功",cartItems.size());
    }

    @GetMapping("/getRegion/{level}")
    @ApiOperation("getRegion")
    public Result getRegion(@PathVariable int level) {
        QueryWrapper<Region> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",level);
        return ResultUtil.success("成功",regionMapper.selectList(queryWrapper));
    }
}
